require "functions"

local tb = {
	[3] = 1,
	[5] = 3,
	[4] = 2,
	[9] = 5,
}

local tbHotNum = {}
for k,v in pairs(tb) do 
	table.insert(tbHotNum,{k,v})
end
table.sort(tbHotNum,function(a,b) 
return a[2] > b[2]
end)
for k,v in pairs(tbHotNum) do 
	print(k,v[1])
end

local tb = {}
local i = 1
repeat
	local tb1 = {i,i+1}
	local tb2 = {i,i+3}
	local tb3 = {i+1,i+1+1}
	local tb4 = {i+1,i+1+3}
	local tb5 = {i+2,i+2+3}
	table.insert(tb,tb1)
	table.insert(tb,tb2)
	table.insert(tb,tb3)
	table.insert(tb,tb4)
	table.insert(tb,tb5)
	i = i + 3
until i==34

for k,v in pairs(tb) do 
	print("{"..v[1],","..v[2],"},")
end

local tb = {}
local i = 1
repeat
	local tb1 = {i,i+1,i+2}
	table.insert(tb,tb1)
	i = i + 3
until i>=36

for k,v in pairs(tb) do 
	print("{"..v[1]..","..v[2]..","..v[3].."},")
end

print("___________4___")
local tb = {}
local i = 1
repeat
	local tb1 = {i,i+1,i+3,i+4}
	local tb2 = {i+1,i+1+1,i+3+1,i+4+1}
	table.insert(tb,tb1)
	table.insert(tb,tb2)
	i = i + 3
until i>=34

for k,v in pairs(tb) do 
	print("{"..v[1]..","..v[2]..","..v[3]..","..v[4].."},")
end


local Rule = class("Rule")

function  Rule:ctor(roomData)
	--随机种子
	-- math.randomseed()
	self.card = {
	
	}
	self.tbCard = {}
	--下注类型，1单，2双，3三个，4四个，5六个，6十二个，7十八个，8红，9黑，	
	--倍数
	self.tbBeiShu={
		36,18,12,9,4,3,3,2,2,2
	}
	--单
	self.betDan={
		0,
		3,6,9,12,15,18,21,24,27,30,33,36,
		2,5,8,11,14,17,20,23,26,29,32,35,
		1,4,7,10,13,16,19,22,25,28,31,34,
	}
	--双
	self.betSuan={
		{0,1},{0,2},{0,3},
		{1	,2	},
		{1	,4	},
		{2	,3	},
		{2	,5	},
		{3	,6	},
		{4	,5	},
		{4	,7	},
		{5	,6	},
		{5	,8	},
		{6	,9	},
		{7	,8	},
		{7	,10	},
		{8	,9	},
		{8	,11	},
		{9	,12	},
		{10	,11	},
		{10	,13	},
		{11	,12	},
		{11	,14	},
		{12	,15	},
		{13	,14	},
		{13	,16	},
		{14	,15	},
		{14	,17	},
		{15	,18	},
		{16	,17	},
		{16	,19	},
		{17	,18	},
		{17	,20	},
		{18	,21	},
		{19	,20	},
		{19	,22	},
		{20	,21	},
		{20	,23	},
		{21	,24	},
		{22	,23	},
		{22	,25	},
		{23	,24	},
		{23	,26	},
		{24	,27	},
		{25	,26	},
		{25	,28	},
		{26	,27	},
		{26	,29	},
		{27	,30	},
		{28	,29	},
		{28	,31	},
		{29	,30	},
		{29	,32	},
		{30	,33	},
		{31	,32	},
		{31	,34	},
		{32	,33	},
		{32	,35	},
		{33	,36	},
		{34	,35	},{35,36},
	}
	--3个
	self.betSan={
		{0,1,2},{0,2,3},
		{1,2,3},
		{4,5,6},
		{7,8,9},
		{10,11,12},
		{13,14,15},
		{16,17,18},
		{19,20,21},
		{22,23,24},
		{25,26,27},
		{28,29,30},
		{31,32,33},
		{34,35,36},
	}
	--4个
	self.betSi = {
		{1,2,4,5},
		{2,3,5,6},
		{4,5,7,8},
		{5,6,8,9},
		{7,8,10,11},
		{8,9,11,12},
		{10,11,13,14},
		{11,12,14,15},
		{13,14,16,17},
		{14,15,17,18},
		{16,17,19,20},
		{17,18,20,21},
		{19,20,22,23},
		{20,21,23,24},
		{22,23,25,26},
		{23,24,26,27},
		{25,26,28,29},
		{26,27,29,30},
		{28,29,31,32},
		{29,30,32,33},
		{31,32,34,35},
		{32,33,35,36},
	}
	--6
	self.betLiu = {
		{1,2,3,4,5,6},
		{7,8,9,10,11,12},
		{13,14,15,16,17,18},
		{19,20,21,22,23,24},
		{25,26,27,28,29,30},
		{31,32,33,34,35,36},
	}
	--12
	self.betShiEr = {
		{1,2,3,4,5,6,7,8,9,10,11,12},
		{13,14,15,16,17,18,19,20,21,22,23,24},
		{25,26,27,28,29,30,31,32,33,34,35,36}
	}
	--18
	self.betShiBa = {
		{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18},
		{19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36}
	}
    --红
    self.betHong = {
	    {3,9,12,18,21,27,30,36,
	    5,14,23,32,
	    1,7,16,19,25,34,}
	}
    --黑
    self.betHei = {
    	{6,15,24,33,
    	2,8,11,17,20,26,29,35,
    	4,10,13,22,28,31,}
	}

	self.allBetType = {
		self.betDan,
		self.betSuan,
		self.betSan,
		self.betSi,
		self.betLiu,
		self.betShiEr,
		self.betShiBa,
		self.betHong,
		self.betHei,
	}

	dump(self.allBetType[2][1])

end

-- local rule = Rule.new()


local function fun(n, ... )
	local arg = {...}
	print(arg[n])
end

-- fun(2,3,4)


local arr = {3,2,3,2,1,5,}
table.sort(arr)
local newarr = {arr[1]}

for i=2,#arr do 
	if arr[i-1] ~= arr[i] then 
		newarr[i] = arr[i]
		print(arr[i])
	end
end	


local function test()
	local i=0
	return function()
		i = i + 1
		return i
	end
end

local c1 = test()
print("___________________闭包______")
print(c1())
print(c1())

print("___________________迭代器______")
local function list_iter(list)
	local n = #list
	local i = 0
	return function()
		i=i+1
		if i<=n then 
			return i,list[i]
		end
	end
end

local list = {3,2,1,5,6,2}
for k,v in list_iter(list) do 
	print(k,v)
end

print("___________________元表______")
local metatable = {
	__index = {2,3,4},
	__newindex = function(tb,a,b)
		-- print("__no index",tb,a,b)
		
	end,
	__call = function()
		print("__call__________")
	end,
	__tostring = function(tb)
		print("_____tostring____")
		local str = ""
		for k,v in pairs(tb) do 
			str = str .. k ..","..tostring(v)
			print("__222_",k)
		end
		return str
	end,
}

local tb = {3}
tb.call = function ( ... )
	print("__tb.call")
end
setmetatable(tb,{
	-- __metatable = true,
	})
setmetatable(tb,metatable)
tb.name = "world"
print(tb[3])
tb[1] = 2
-- tb() --__call
print(tb)

print(rawget(tb,1))

print("___________________协程______")
local function fun(_cor)
	print("coroutine fun")
	if _cor then 
		coroutine.status(_cor)
		coroutine.yield(_cor)
	end
end

local cor = coroutine.create(fun)
print(cor)
print(coroutine.status(cor))
coroutine.resume(cor,cor)
coroutine.resume(cor,cor)
print(coroutine.status(cor))

local cor2 = coroutine.wrap(fun)
cor2()
-- print(coroutine.status(cor2))

--以协同程序实现Lua迭代器

local tab = { 1,2,3,4,5,6 }

function corFun( tab,num )
     num = num or 0
     return function()
          while true do
               num = num +1
               if( num > #tab ) then return end
               coroutine.yield(tab[num])
          end
     end
end

function cor( tab )
     local cor = coroutine.create( corFun( tab ) )
     return function ()--迭代器
          local status,value = coroutine.resume( cor )
          return value
     end
end

for v in cor( tab ) do
     if v then
          print(v)
     else
          break
     end
end

tb = {
	[1] = {22,},
	[5] = {1,3},
}

print(tb[5][2])

require "collider"
